import sys
from PyQt5.QtCore import pyqtSignal,pyqtSlot,QThread,Qt
from PyQt5.QtWidgets import QMainWindow,QApplication,QInputDialog,QFileDialog,\
     QMessageBox,QColorDialog,QListWidgetItem,QTableWidgetItem,QProgressBar,QRadioButton,QMdiSubWindow,QWidget,QMdiArea

import sqlite3, os
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

from PyQt5.uic import loadUi

import model
import sql_hems

class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        loadUi('main.ui', self)

        # 初始化工程文件信息
        self.proj_hems = None

        # 初始化模型显示的Mdi界面
        self.model = model.Form()
        self.model.root = self
        self.model.closeEvent = lambda event:self.actionModel.setChecked(False)
        sub = QMdiSubWindow(self.mdiArea)
        self.model.submdi = sub
        sub.setWidget(self.model)
        self.model.showMaximized()

        # # 初始数据库增删改查的界面
        # self.sql_hems = sql_hems.Form(1)
        # self.sql_hems.root = self
        # self.sql_hems.closeEvent = lambda event: self.actionSQL.setChecked(False)
        # sub = QMdiSubWindow(self.mdiArea)
        # self.sql_hems.submdi = sub
        # sub.setWidget(self.sql_hems)

    """-----------主界面的一些信号控制---------------"""
    @pyqtSlot()
    def on_actionModel_triggered(self):
        if self.sender().isChecked():
            self.model.show()
            self.model.setFocus()
        else:
            self.model.close()
            self.model.submdi.close()

    @pyqtSlot()
    def on_actionSQL_triggered(self):
        if self.sender().isChecked():
            self.sql_hems.show()
            self.sql_hems.setFocus()
        else:
            self.sql_hems.close()
            self.sql_hems.submdi.close()

    @pyqtSlot()
    def on_actionTile_triggered(self):
        self.mdiArea.tileSubWindows()

    @pyqtSlot()
    def on_actionCascade_triggered(self):
        self.mdiArea.cascadeSubWindows()

    @pyqtSlot()
    def on_actionClose_All_triggered(self):
        self.mdiArea.closeAllSubWindows()

    @pyqtSlot()
    def on_actionAbout_triggered(self):
        QMessageBox.about(self, "关于",
                          """
                          """)

    # --------模型和网格相关的按钮--↓---------------------------------------------
    @pyqtSlot()
    def on_action_createProj_triggered(self):
        fileName = QFileDialog.getSaveFileName()[0]
        fileDataBaseName = os.path.splitext(fileName)[0]
        self.proj_hems = sql_hems.Form(0)
        self.proj_hems.create_db(db_name=fileDataBaseName)   #默认创建一个默认有PO的table

    @pyqtSlot()
    def on_actionImportProj_triggered(self):
        filter = '''SQLITE(*.sqlite)'''
        fileName = QFileDialog.getOpenFileName(filter=filter)[0]
        if fileName:
            conn = sqlite3.connect(fileName)
            cursor = conn.execute("SELECT * from sqlite_sequence")

            self.proj_hems = sql_hems.Form(0)
            fileDataBaseName = os.path.splitext(fileName)[0]
            self.proj_hems.db_name = fileDataBaseName
            for it in cursor:
                self.proj_hems.db_table_name = it[0] #读取数据库汇总第一个表作为默认表
                break
            self.proj_hems.db_list = []
            self.proj_hems.db_list.append(QSqlDatabase.addDatabase('QSQLITE', self.proj_hems.db_name))
            query = QSqlQuery(self.proj_hems.db_list[-1])
            self.SqlTableModel = query      #让SqlTableModel非空
        else:
            QMessageBox.information(self, "提示", "请重新导入工程")

    @pyqtSlot()
    def on_actionImportModel_triggered(self):
        self.model.on_actionImportModel_triggered()

    @pyqtSlot()
    def on_actionExportModel_triggered(self):
        self.model.on_actionExportModel_triggered()

    @pyqtSlot()
    def on_actionImportMesh_triggered(self):
        self.model.on_actionImportMesh_triggered()

    @pyqtSlot()
    def on_actionGenerateMesh_triggered(self):
        self.model.on_actionGenerateMesh_triggered()

    @pyqtSlot()
    def on_actionExportMesh_triggered(self):
        self.model.on_actionExportMesh_triggered()

    @pyqtSlot()
    def on_actionExtractEdges_triggered(self):
        self.model.on_actionExtractEdges_triggered()

    @pyqtSlot()
    def on_actionClearMesh_triggered(self):
        self.model.on_actionClearMesh_triggered()

    @pyqtSlot()
    def on_actionTargetInformation_triggered(self):
        self.model.on_actionTargetInformation_triggered()

# --------与建模和模型处理相关函数--↓---------------------------------------------
    @pyqtSlot()
    def on_actionGenerateBox_triggered(self):
        self.model.on_actionGenerateBox_triggered()

    @pyqtSlot()
    def on_actionGenerateSphere_triggered(self):
        self.model.on_actionGenerateSphere_triggered()

    @pyqtSlot()
    def on_actionGenerateTorus_triggered(self):
        self.model.on_actionGenerateTorus_triggered()

    @pyqtSlot()
    def on_actionGenerateCone_triggered(self):
        self.model.on_actionGenerateCone_triggered()

    @pyqtSlot()
    def on_actionGenerateCylinder_triggered(self):
        self.model.on_actionGenerateCylinder_triggered()

    @pyqtSlot()
    def on_actionGenerateWedge_triggered(self):
        self.model.on_actionGenerateWedge_triggered()

    @pyqtSlot()
    def on_actionBoolCut_triggered(self):
        self.model.on_actionBoolCut_triggered()

    @pyqtSlot()
    def on_actionBoolFuse_triggered(self):
        self.model.on_actionBoolFuse_triggered()

    @pyqtSlot()
    def on_actionBoolCommon_triggered(self):
        self.model.on_actionBoolCommon_triggered()

    @pyqtSlot()
    def on_actionGeneratePoint_triggered(self):
        self.model.on_actionGeneratePoint_triggered()

    @pyqtSlot()
    def on_actionGenerateLine_triggered(self):
        self.model.on_actionGenerateLine_triggered()

    @pyqtSlot()
    def on_actionGenerate_face_triggered(self):
        self.model.on_actionGenerate_face_triggered()

    @pyqtSlot()
    def on_actionGenerateCircle_triggered(self):
        self.model.on_actionGenerateCircle_triggered()

    @pyqtSlot()
    def on_actionGenerate_2D_rect_triggered(self):
        self.model.on_actionGenerate_2D_rect_triggered()

    @pyqtSlot()
    def on_actionPrism_triggered(self):
        self.model.on_actionPrism_triggered()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
